package com.test.html;

import okhttp3.*;
import okio.BufferedSink;
import okio.Okio;
import okio.Sink;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 下载文件
 */
public class HtmlParser4 {
    private static AtomicInteger handled = new AtomicInteger(0);

    private static LocalDate now = LocalDate.now();
    private static String date = now.format(DateTimeFormatter.ofPattern("yy-MM"));
    private static String baseDir = "D:\\dl\\" + date;
    private static final OkHttpClient client = new OkHttpClient();

    // 信号量：6，公平锁
    private static Semaphore semaphore = new Semaphore(6, true);

    public static void main(String[] args) throws IOException {
        System.out.println(baseDir);
        Path path = Paths.get(baseDir);
        Files.createDirectories(path);

        Path jsonPath = Paths.get("D:\\dl\\list.txt");
//        https://img.chtfund.com/group2/M00/00/0C/CmQVXFt4EoiASvDUAAyVV8TwpPo181.png,abc.png
//        https://img.chtfund.com/group2/M00/00/0C/CmQVXFt4EoiASvDUAAyVV8TwpPo181.png,123.png
//        https://img.chtfund.com/group2/M00/00/0C/CmQVXFt4EoiASvDUAAyVV8TwpPo181.png,456.png
//        http://10.70.5.97:8888/group1/M00/01/15/CkYFZmTLSeGAR-A5AAF3ZEUfY7k240.pdf,1062747_20230803143201274.pdf
        Scanner scanner = new Scanner(jsonPath);
        String line = "";
        String imageUrl = "";
        String fileName = "";
        int total = 0;
        while (scanner.hasNextLine()) {
            line = scanner.next();
            System.out.println("处理：" + line);
            if (StringUtils.isBlank(line)) {
                break;
            }
            imageUrl = line.split(",")[0];
            fileName = line.split(",")[1];
            if (StringUtils.isBlank(fileName)) {
                continue;
            }
            total++;
            try {
                semaphore.acquire();
                downloadFile(imageUrl, fileName);
            } catch (InterruptedException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        while (handled.get() < total) {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        System.out.println("处理文件数：" + handled.get());
        // System.exit(1);
    }

    //下载文件
    private static void downloadFile(final String url, String name) {
        new Thread(() -> download(url, name)).start();
    }

    //下载

    public static void download(final String url, String name) {
        Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                handled.incrementAndGet();
                // 下载失败
                e.printStackTrace();
                System.out.println("DOWNLOAD" + "download failed");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Sink sink = null;
                BufferedSink bufferedSink = null;
                try {
                    final long startTime = System.currentTimeMillis();
                    File dest = new File(baseDir, name);
                    if (dest.exists()) {
                        System.out.println(url + "已存在");
                        return;
                    }
                    sink = Okio.sink(dest);
                    bufferedSink = Okio.buffer(sink);
                    bufferedSink.writeAll(response.body().source());
                    bufferedSink.close();
                    System.out.println(url + " download success " + "totalTime=" + (System.currentTimeMillis() - startTime));
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("DOWNLOAD" + "download failed");
                } finally {
                    handled.incrementAndGet();
                    if (bufferedSink != null) {
                        bufferedSink.close();
                    }
                    response.close();
                }
            }
        });
    }
}
